package com.study.samsung.Control;

import com.alibaba.fastjson.JSONObject;
import com.study.samsung.Entity.Commodity;
import com.study.samsung.Entity.Succession;
import com.study.samsung.Entity.User;
import com.study.samsung.Service.GoodsService;
import com.study.samsung.Service.IndexSearchService;
import com.study.samsung.Service.PaymentService;
import com.study.samsung.Utils.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/payment")
public class PaymentServlet extends BaseServlet {
    PaymentService ps = new PaymentService();
    IndexSearchService iss = new IndexSearchService();
    GoodsService gds = new GoodsService();
    @Override
    public String excute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        List<Commodity> newgoods = iss.selectnew();
        List<Succession> selectxl = iss.selectxl();

        request.setAttribute("newgoods",newgoods);
        request.setAttribute("selectxl",selectxl);

        Object user = session.getAttribute("user");
        if (user!=null){
            User nowuser = (User) user;
            Integer uid = nowuser.getUid();

            session.setAttribute("cartcount",gds.cartcount(uid));
        }
        return "f:payment";
    }

    public String selectAll(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        //设置响应内容的主体格式，是json格式的
        response.setContentType("application/json;charset=utf-8");
        Integer did = (Integer) session.getAttribute("did");
        System.out.println(did);
        List<Commodity> list = ps.selectAll(did);
        for (Commodity commodity : list) {
            System.out.println(commodity.getSxid());
        }
        String json = JSONObject.toJSONString(list);
        System.out.println(json);
        return json;

    }

    public String update(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String num1 = request.getParameter("a");
        String num2 = request.getParameter("b");
        String num3 = request.getParameter("c");
        Integer did = (Integer) session.getAttribute("did");

        if (num3==null){
            if (num1==null&&num2!=null){
                Integer integer = ps.update(did,"待付款");
            }else if (num1!=null&&num2==null){
                Integer integer = ps.update(did,"待发货");
                List<Commodity> list = ps.selectAll(did);
                for (Commodity commodity : list) {
                    Integer del = ps.deleteCart(commodity.getSxid());
                }
            }
        }else{
            Integer integer = ps.update(did,"已取消");
        }
        return null;
    }

}
